<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${table.basePackage}.mapper.${table.entityName}Mapper">
    <!-- ${table.comment!table.entityName} | table: ${table.tableName} | template: multi (join-ready) -->

    <resultMap id="BaseResultMap" type="${table.basePackage}.entity.${table.entityName}">
        <#-- 先输出所有主键字段（<id>元素必须在<result>元素之前） -->
        <#list columns as c>
        <#if c.isPk?? && c.isPk>
        <id property="${c.propertyName!'id'}" column="${c.columnName!'id'}" />
        </#if>
        </#list>
        <#-- 再输出所有非主键字段 -->
        <#list columns as c>
        <#if !(c.isPk?? && c.isPk)>
        <result property="${c.propertyName!'id'}" column="${c.columnName!'id'}" />
        </#if>
        </#list>
    </resultMap>

    <sql id="Base_Column_List">
        <#list columns as c>t.<#if c.columnName??>${c.columnName}<#else>id</#if><#if c_has_next>, </#if></#list>
    </sql>

    <!-- join config via table.ext/joinDefs
      joinDefs: [ { alias: 'u', table: 'sys_user', on: 't.user_id = u.id', columns: 'u.username as username' } ]
    -->

    <select id="selectPageJoin" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        <#-- extra columns from join defs -->
        <#if table.ext?? && table.ext.joinDefs??>
        , <#list table.ext.joinDefs as j>${j.columns}<#if j_has_next>, </#if></#list>
        </#if>
        from ${table.tableName} t
        <#if table.ext?? && table.ext.joinDefs??>
        <#list table.ext.joinDefs as j>
        left join ${j.table} ${j.alias} on ${j.on}
        </#list>
        </#if>
        <where>
        <#list columns as c>
          <#-- 跳过BaseEntity中已有的字段，但允许查询条件 -->
          <#assign skipField = false>
          <#if c.columnName?? && (c.columnName?lower_case == "create_by" || c.columnName?lower_case == "update_by")>
            <#assign skipField = true>
          </#if>
          
          <#if c.columnName?? && c.propertyName?? && !skipField>
            <#if (c.javaType?? && c.javaType?contains('String'))>
              <if test="f.${c.propertyName} != null and f.${c.propertyName} != ''">
                and t.${c.columnName} like concat('%', #{f.${c.propertyName}}, '%')
              </if>
            <#else>
              <if test="f.${c.propertyName} != null">
                and t.${c.columnName} = #{f.${c.propertyName}}
              </if>
            </#if>
          </#if>
        </#list>
        </where>
        order by t.id desc
    </select>

    <select id="selectListJoin" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        <#if table.ext?? && table.ext.joinDefs??>
        , <#list table.ext.joinDefs as j>${j.columns}<#if j_has_next>, </#if></#list>
        </#if>
        from ${table.tableName} t
        <#if table.ext?? && table.ext.joinDefs??>
        <#list table.ext.joinDefs as j>
        left join ${j.table} ${j.alias} on ${j.on}
        </#list>
        </#if>
        <where>
        <#list columns as c>
          <#-- 跳过BaseEntity中已有的字段，但允许查询条件 -->
          <#assign skipField = false>
          <#if c.columnName?? && (c.columnName?lower_case == "create_by" || c.columnName?lower_case == "update_by")>
            <#assign skipField = true>
          </#if>
          
          <#if c.columnName?? && c.propertyName?? && !skipField>
            <#if (c.javaType?? && c.javaType?contains('String'))>
              <if test="f.${c.propertyName} != null and f.${c.propertyName} != ''">
                and t.${c.columnName} like concat('%', #{f.${c.propertyName}}, '%')
              </if>
            <#else>
              <if test="f.${c.propertyName} != null">
                and t.${c.columnName} = #{f.${c.propertyName}}
              </if>
            </#if>
          </#if>
        </#list>
        </where>
        order by t.id desc
    </select>

    <#-- relations insert/delete, driven by table.ext.relations: [{alias:'items', table:'order_item', columns:'order_id,sku,qty', values: '#{mainId},#{data.sku},#{data.qty}', where:'order_id = #{mainId}'}] -->
    <#if table.ext?? && table.ext.relations??>
    <#list table.ext.relations as r>
    <insert id="insertRel_${r.alias}">
        insert into ${r.table} (${r.columns}) values (${r.values})
    </insert>
    <delete id="deleteRel_${r.alias}">
        delete from ${r.table} where ${r.where!('1=0')}
    </delete>
    </#list>
    </#if>
</mapper>


